package com.example.ch02_student;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.ch02_student.adminsDo.StudentInfo;
import com.example.ch02_student.entity.Course;
import com.example.ch02_student.entity.Grade;
import com.example.ch02_student.entity.Student;
import com.example.ch02_student.utils.DataBean;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StudentMain extends AppCompatActivity {
    private OkHttpClient okHttpClient;
    private ListView lv;
    private List<Map<String, String>> list;
    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_main);
        okHttpClient=new OkHttpClient();
        lv=findViewById(R.id.lv);
        //
        Student student = (Student)getIntent().getSerializableExtra("student");
        init(student.getSno());

    }

    //初始化页面
    public void init(String sno) {
        //selectAllGrade
        //班级 专业 学院
        Request build = new Request.Builder().url(UrlBase.URL_BASE + "/student/selectAllGrade?sno="+sno).build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                xuanran(string);
            }
        });
    }

    //数据渲染
    public void xuanran(String json) {
        //解析
        ResultVo resultVo = new Gson().fromJson(json, ResultVo.class);
        List<DataBean> data = resultVo.getData();
        list = new ArrayList<>();
        Map<String, String> map;
        System.err.println(data);
        if (data != null && data.size() > 0) {
            final String[] s = new String[]{"sno", "sname", "sex", "cclass", "major", "tel",
                    "college", "age", "createTime", "cname0", "cname1", "cname2", "cname3", "grade0", "grade1", "grade2", "grade3"};
            final int[] ints = new int[]{R.id.sno, R.id.sname, R.id.ssex, R.id.scclass, R.id.smajor
                    , R.id.stel, R.id.scollege, R.id.sage, R.id.screateTime, R.id.course1, R.id.course2, R.id.course3, R.id.course4
                    , R.id.grade1, R.id.grade2, R.id.grade3, R.id.grade4};

            for (int i = 0; i < data.size(); i++) {
                //得到每个StudentVo
                DataBean dataBean = data.get(i);
                map = new HashMap<>();
                map.put("sno", dataBean.getSno());
                map.put("sname", dataBean.getSname());
                map.put("sex", dataBean.getSex());
                map.put("cclass", dataBean.getCclass());
                map.put("major", dataBean.getMajor());
                map.put("tel", dataBean.getTel());
                map.put("college", dataBean.getCollege());
                map.put("age", dataBean.getAge() + "");
                if (dataBean.getCreateTime() != null) {
                    map.put("createTime", simpleDateFormat.format(dataBean.getCreateTime()));
                }

                //填充课程名称及分数
                for (int j = 0; j < dataBean.getCourses().size(); j++) {
                    Course course = dataBean.getCourses().get(j);
                    map.put("cname" + j, course.getCname() + "(" + course.getCno() + ")");
                }
                for (int k = 0; k < dataBean.getGrades().size(); k++) {
                    Grade grade = dataBean.getGrades().get(k);
                    map.put("grade" + k, grade.getScore() + "");
                }
                list.add(map);
                //准备simpleAdapter数据
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SimpleAdapter simpleAdapter = new SimpleAdapter(StudentMain.this, list, R.layout.activity_student_info_items, s, ints);
                        lv.setAdapter(simpleAdapter);
                    }
                });
                System.err.println(list);

            }
        }
    }
}
